/**
 * 
 */
package cx3k.GUI;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.*;

import cx3k.dataaccess.search.Searcher;
import cx3k.dataaccess.search.Searcher.SearchField;
import cx3k.scheduler.objects.Course;

/**
 * @author Chris
 *
 */
public class SearchOptionsPane extends JPanel {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JButton jbAll, jbRequired;
	private JComboBox jcbMajor;
	private JCheckBox jcb1, jcb2, jcb3, jcb4;
	private JCheckBox jcbMon, jcbTues, jcbWed, jcbThurs, jcbFri;
	private Searcher searcher;
	private String[] majors;
	private SearchResultsPane resultPane;
	
	/**
	 * 
	 * @param searcher Searcher
	 */
	public SearchOptionsPane(Searcher searcher, SearchResultsPane results) {
		this.setBackground(MainWindow.MAINCOLOR);
		this.setLayout(new GridLayout(4, 1));
		Box MajorBox = Box.createHorizontalBox();
		Box DayBox = Box.createHorizontalBox();
		Box LevelBox = Box.createHorizontalBox();
		MajorListener ML = new MajorListener();
		LevelListener LL = new LevelListener();
		DayListener DL = new DayListener();
		this.searcher = searcher;
		this.majors = Searcher.getDegreeProgramNames();
		this.resultPane = results;
		
		jcbMajor = new JComboBox();
		jcbMajor.setCursor(new Cursor(Cursor.HAND_CURSOR));
		this.addToComboBox(this.majors,	jcbMajor);
		jcbMajor.addItemListener(ML);
		
		jbAll = new JButton("All ");
		initalizeButton(jbAll);
		jbAll.addActionListener(ML);
		jbRequired = new JButton(" Required ");
		initalizeButton(jbRequired);
		jbRequired.addActionListener(ML);
		
		
		MajorBox.add(new JLabel("  Major:   "));
		MajorBox.add(jcbMajor);
		MajorBox.add(Box.createHorizontalGlue());
		this.add(MajorBox);
		
		jcb1 = new JCheckBox();
		initalizeCheckBox(jcb1);
		jcb1.addActionListener(LL);
		jcb2 = new JCheckBox();
		initalizeCheckBox(jcb2);
		jcb2.addActionListener(LL);
		jcb3 = new JCheckBox();
		initalizeCheckBox(jcb3);
		jcb3.addActionListener(LL);
		jcb4 = new JCheckBox();
		initalizeCheckBox(jcb4);
		jcb4.addActionListener(LL);
		
		LevelBox.add(new JLabel("Level: "));
		LevelBox.add(jcb1);
		LevelBox.add(new JLabel("100 "));
		LevelBox.add(jcb2);
		LevelBox.add(new JLabel("200 "));
		LevelBox.add(jcb3);
		LevelBox.add(new JLabel("300 "));
		LevelBox.add(jcb4);
		LevelBox.add(new JLabel("400"));
		this.add(LevelBox);
		
		
		jcbMon = new JCheckBox();
		initalizeCheckBox(jcbMon);
		jcbMon.addActionListener(DL);
		jcbTues = new JCheckBox();
		initalizeCheckBox(jcbTues);
		jcbTues.addActionListener(DL);
		jcbWed = new JCheckBox();
		initalizeCheckBox(jcbWed);
		jcbWed.addActionListener(DL);
		jcbThurs = new JCheckBox();
		initalizeCheckBox(jcbThurs);
		jcbThurs.addActionListener(DL);
		jcbFri = new JCheckBox();
		initalizeCheckBox(jcbFri);
		jcbFri.addActionListener(DL);
		
		JLabel jlMon = new JLabel(" M  ");
		JLabel jlTues = new JLabel(" Tu  ");
		JLabel jlWed = new JLabel(" W  ");
		JLabel jlThurs = new JLabel(" Th  ");
		JLabel jlFri = new JLabel(" F");
		
		DayBox.add(jcbMon);
		DayBox.add(jlMon);
		DayBox.add(jcbTues);
		DayBox.add(jlTues);
		DayBox.add(jcbWed);
		DayBox.add(jlWed);
		DayBox.add(jcbThurs);
		DayBox.add(jlThurs);
		DayBox.add(jcbFri);
		DayBox.add(jlFri);
		this.add(DayBox);
		
		searcher.searchForSmallerValue(SearchField.LEVEL, "500");
		searcher.searchForValueNotLike(SearchField.DAY, "TBA");
		
		
	}
	
	/**
	 * 
	 * @param b
	 */
	private void initalizeButton(JButton b) {
		b.setBackground(MainWindow.MAINCOLOR);
		b.setBorder(null);
		b.setCursor(new Cursor(Cursor.HAND_CURSOR));
	}
	
	/**
	 * Initalizes a check box to conform to program.
	 * @param cb
	 */
	private void initalizeCheckBox(JCheckBox cb) {
		cb.setBackground(MainWindow.MAINCOLOR);
		cb.doClick();
		cb.setCursor(new Cursor(Cursor.HAND_CURSOR));
	}
	
	private void addToComboBox(String s[], JComboBox jcb) {
		jcb.addItem(" ");
		for (int i = 0; i < s.length; i++) {
			if (!s[i].equalsIgnoreCase("CMIS")) jcb.addItem(s[i]);
		}
	}
	
	private void getResults() {
		resultPane.clear();
		if (this.jcb1.isSelected()) resultPane.showResults(toSingleList(get100s()));
		if (this.jcb2.isSelected())resultPane.showResults(toSingleList(get200s()));
		if (this.jcb3.isSelected())resultPane.showResults(toSingleList(get300s()));
		if (this.jcb4.isSelected())resultPane.showResults(toSingleList(get400s()));
	}
	
	private ArrayList<Course> toSingleList(List<?> results) {
		ArrayList<Course> courses = new ArrayList<Course>();
		
		int i = 0;
		for (i = 0; i<results.size(); i++) {
			if (results.get(i).getClass() == Course.class) {
				courses.add((Course) results.get(i));
			}
			else {
				Object[] c = (Object[]) results.get(i);
				courses.add((Course) c[0]);
			}
		}
		
		return courses;
	}
	
	private List<?> get100s() {
		searcher.searchForValueBetween(SearchField.LEVEL, "100", "200");
		List<?> results = searcher.execute();
		searcher.remove(SearchField.LEVEL, "100");
		searcher.remove(SearchField.LEVEL, "200");
		return results;
	}
	
	private List<?> get200s() {
		searcher.searchForValueBetween(SearchField.LEVEL, "199", "300");
		List<?> results = searcher.execute();
		searcher.remove(SearchField.LEVEL, "199");
		searcher.remove(SearchField.LEVEL, "300");
		return results;
	}
	
	private List<?> get300s() {
		searcher.searchForValueBetween(SearchField.LEVEL, "299", "400");
		List<?> results = searcher.execute();
		searcher.remove(SearchField.LEVEL, "299");
		searcher.remove(SearchField.LEVEL, "400");
		return results;
	}
	
	private List<?> get400s() {
		searcher.searchForValueBetween(SearchField.LEVEL, "399", "500");
		List<?> results = searcher.execute();
		searcher.remove(SearchField.LEVEL, "399");
		searcher.remove(SearchField.LEVEL, "500");
		return results;
	}

	
	/**
	 * Class used to respond to options related to major
	 * @author Chris
	 *
	 */
	private class MajorListener implements ActionListener, ItemListener {

		@Override
		public void actionPerformed(ActionEvent ae) {
			// TODO Auto-generated method stub
			
			if (ae.getSource() == jbAll) {
				if (jbAll.getForeground() != Color.RED) {
					jcbMajor.setSelectedIndex(0);
					jbAll.setForeground(Color.RED);
					jbRequired.setForeground(Color.black);
					System.out.println("Class: All");
					//searcher.searchForValueLike(SearchField.REQUIRED, "false");
					searcher.execute();
				}
				else {
					jbAll.setForeground(Color.BLACK);
					searcher.searchForValueLike(SearchField.REQUIRED, "true");
					searcher.execute();
				}
			}
			else if (ae.getSource() == jbRequired) {
				if (jbRequired.getForeground() != Color.RED) {
					jcbMajor.setSelectedIndex(0);
					jbAll.setForeground(Color.black);
					jbRequired.setForeground(Color.RED);
					System.out.println("Class: Required");
					searcher.searchForValueLike(SearchField.REQUIRED, "true");
					searcher.execute();
				}
				else {
					jbRequired.setForeground(Color.BLACK);
					searcher.searchForValueLike(SearchField.REQUIRED, "false");
					searcher.execute();
				}
			}
		}

		@Override
		public void itemStateChanged(ItemEvent ie) {
			// TODO Auto-generated method stub
			if (ie.getSource() == jcbMajor) {
				jbAll.setForeground(Color.black);
				jbRequired.setForeground(Color.black);
				if (jcbMajor.getSelectedIndex() != 0) {
					searcher.clearField(SearchField.DISCIPLINE);
					searcher.searchForExactValue(SearchField.DISCIPLINE, jcbMajor.getSelectedItem().toString());
					getResults();
				}
				else {
					searcher.clearField(SearchField.DISCIPLINE);
					getResults();
				}
			}
		}
		
	}
	
	/**
	 * Class used to respond to options related to the Levels
	 * @author Chris
	 *
	 */
	private class LevelListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent ae) {
			getResults();
		}
		
	}
	
	
	/**
	 * Class used to respond to options related to the days.
	 * @author Chris
	 *
	 */
	private class DayListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent ae) {
			// TODO Auto-generated method stub
			if (ae.getSource() == jcbMon) {
				if (jcbMon.isSelected()) searcher.remove(SearchField.DAY, Searcher.MONDAY);
				else searcher.searchForValueNotLike(SearchField.DAY, Searcher.MONDAY);
				getResults();
			}
			else if (ae.getSource() == jcbTues) {
				if (jcbTues.isSelected()) searcher.remove(SearchField.DAY, Searcher.TUESDAY);
				else searcher.searchForValueNotLike(SearchField.DAY, Searcher.TUESDAY);
				getResults();
			}
			else if (ae.getSource() == jcbWed) {
				if (jcbWed.isSelected()) searcher.remove(SearchField.DAY, Searcher.WEDNESDAY);
				else searcher.searchForValueNotLike(SearchField.DAY, Searcher.WEDNESDAY);
				getResults();
			}
			else if (ae.getSource() == jcbThurs) {
				if (jcbThurs.isSelected()) searcher.remove(SearchField.DAY, Searcher.THURSDAY);
				else searcher.searchForValueNotLike(SearchField.DAY, Searcher.THURSDAY);
				getResults();
			}
			else if (ae.getSource() == jcbFri) {
				if (jcbFri.isSelected()) searcher.remove(SearchField.DAY, Searcher.FRIDAY);
				else searcher.searchForValueNotLike(SearchField.DAY, Searcher.FRIDAY);
				getResults();
			}
		}
		
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
